<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller\car;

use app\admin\model\Adminlog as AdminlogModel;
use app\admin\model\Area;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\Car;
use app\admin\model\Staff;
use app\common\controller\Adminbase;
use think\Db;
use util\Tree;

/**
 * 权限管理控制器
 */
class Index extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new Car();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = $this->modelClass
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params             = $this->request->post('');
            $result             = $this->validate($params, 'Car');
            if (true !== $result) {
                return $this->error($result);
            }
            try {
                $this->modelClass->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("添加成功！", url('index'));
        }
        $area = Area::where(['status' => 1])->field('id,name')->select()->toArray();
        $this->assign('area',$area);
        return $this->fetch();
    }


    public function edit()
    {
        $id  = $this->request->param('id/d', 0);
        $row = $this->modelClass->get($id);
        if (!$row) {
            $this->error('记录未找到');
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post('');
            $result = $this->validate($params, 'Car');
            if (true !== $result) {
                return $this->error($result);
            }
            try {
                $row->allowField(true)->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("修改成功！");
        }
        $area = Area::where(['status' => 1])->field('id,name')->select()->toArray();
        $this->assign('area',$area);
        $this->assign("data", $row);
        return $this->fetch();
    }





}
